import Vue from 'vue';
import Router from 'vue-router';
import routerConfig from '@/config/routes';
import {getToken} from "./utils/auth";
import {getUser} from "./utils/localStorageData";

Vue.use(Router);
// 解决点击同一路由多次报错
//获取原型对象上的push函数
const originalPush = Router.prototype.push
//修改原型对象中的push方法
Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

let beforeEach = function (to, from, next) {
    console.log(to.name)
}

var router = new Router({
  routes: routerConfig,
})

router.beforeEach((to, from, next)=>{
  if(to.path=="/downloadApp.html"){
    next({path:"/appLogin"})
    return
  }
  if (to.name == "login" || to.name == "register" || to.name == "enter" || to.name == "appLogin"){
    next();
  }else {
    let token = getToken();
    let user = getUser();
    if (!token || token.length == 0 || !user.id){
      next({name:"login",query: { backUrl: to.name }})
    }else {
      if (to.fullPath == "/patient"){
        next({name:'patientList',query:{type:0,t:new Date().getTime()}});
      }else {
        next();
      }

    }
  }
})

export default router;
